<template>
  <div class="login-container">
    <div class="login">
      <div class="login-title">用户登录</div>
      <div class="phone">
        <span>手机号</span>
        <input v-model="phone" type="text" placeholder="请输入手机号" />
      </div>
      <div class="pwd">
        <span>密码</span>
        <input v-model="pwd" type="password" placeholder="请输入密码" />
      </div>
      <button class="login-button" @click="loginApi">登录</button>
    </div>
  </div>
</template>

<script>
import { login } from '@/api'
import { Toast } from '@/components/Toast'
import { mapGetters, mapActions } from 'vuex'
export default {
  name: 'Login',
  data() {
    return {
      phone: '',
      pwd: ''
    }
  },

  computed: {
    ...mapGetters({
      userInfo: 'user/getUser'
    })
  },

  methods: {
    ...mapActions({
      changeUser: 'user/changeUser'
    }),

    // 登录
    async loginApi() {
      const { phone, pwd } = this
      const res = await login({
        phone,
        pwd
      })
      if (res.code === 200) {
        Toast({
          type: 'success',
          text: '登录成功'
        })
        sessionStorage.setItem('user', JSON.stringify(res.data))
        this.changeUser(res.data)
        this.$router.push('/')
      } else {
        Toast({
          type: 'warning',
          text: '登录异常'
        })
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  max-width: 980px;
  margin: 0 auto 0;
  padding-top: 20px;
  box-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
  border-radius: 2px;
}

.login {
  padding: 20px 0 100px 30px;
  background-color: #fff;
  border-radius: 2px;
}
.title {
  font-size: 18px;
}
.phone > span,
.pwd > span {
  display: inline-block;
  text-align: right;
  width: 70px;
  margin-right: 15px;
}
.phone > input,
.pwd > input {
  width: 320px;
  height: 20px;
  padding: 5px;
  border-radius: 3px;
  border: 1px solid #ccc;
  font-size: 14px;
}
input:focus {
  outline: none;
}
.phone {
  margin-top: 30px;
}
.pwd {
  margin-top: 20px;
}
.login-button {
  margin-left: 85px;
  margin-top: 20px;
  padding: 4px 8px;
  color: #333;
  background-color: #fff;
  font-size: 14px;
  border: 1px solid rgba(80, 80, 90, 0.2);
  border-radius: 3px;
  box-shadow: 0 1px 0 rgba(66, 66, 77, 0.1);
  cursor: pointer;
}
.login-button:focus {
  outline: none;
}
.login-button:active {
  background-color: #e9e9e9;
}
</style>
